package com.scujj.cs6.aop;


import com.scujj.cs6.annotation.AuthCheck;
import com.scujj.cs6.utils.JwtTool;
import jakarta.servlet.http.HttpServletRequest;
import lombok.AllArgsConstructor;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;


/**
 * 权限校验 AOP
 *
 * @author FlanChan
 */
@Aspect
@Component
@AllArgsConstructor
public class AuthInterceptor {

    private final HttpServletRequest request;

    private final JwtTool tool;

    /**
     * 执行拦截
     *
     * @param joinPoint
     * @param authCheck
     * @return
     */
    @Around("@annotation(authCheck)")
    public Object doInterceptor(ProceedingJoinPoint joinPoint, AuthCheck authCheck) throws Throwable {
        Long l = tool.parseToken(request.getHeader("Authorization"));
        request.setAttribute("user",l);
        // 通过权限校验，放行
        return joinPoint.proceed();
    }
}

